package com.alibaba.druid.pool;

import com.alibaba.druid.util.jdbc.ResultSetMetaDataBase;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by allwefantasy on 8/31/16.
 */
public class ElasticSearchResultSetMetaDataBase extends ResultSetMetaDataBase {

/*    public ElasticSearchResultSetMetaDataBase(List<String> headers) {
        ColumnMetaData columnMetaData;
        for (String column : headers) {
            columnMetaData = new ColumnMetaData();
            columnMetaData.setColumnLabel(column);
            columnMetaData.setColumnName(column);
            getColumns().add(columnMetaData);
        }
    }*/
    private final List<ColumnMetaData> columns = new ArrayList<>();

  public ElasticSearchResultSetMetaDataBase(List<String> headers, List<List<Object>> lines) {
        setColumnHeader(headers);
        setColumnTypeName(this.columns, lines);
    }

        private void setColumnHeader(List<String> headers) {
        for (String column : headers) {
            ResultSetMetaDataBase.ColumnMetaData columnMetaData = new ResultSetMetaDataBase.ColumnMetaData();
            columnMetaData.setColumnLabel(column);
            columnMetaData.setColumnName(column);
            this.columns.add(columnMetaData);
        }
    }

    private void setDefaultColumnTypeName() {
        for (ResultSetMetaDataBase.ColumnMetaData columnMetaData : this.columns) {
            columnMetaData.setColumnType(12);
            columnMetaData.setColumnTypeName("VARCHAR");
        }
    }


    private void setColumnTypeName(List<ResultSetMetaDataBase.ColumnMetaData> columns, List<List<Object>> lines)
    {
        if (lines.size() == 0) {
            setDefaultColumnTypeName();
            return;
        }

        List line = (List)lines.get(0);
        if (line.size() == 0) {
            setDefaultColumnTypeName();
            return;
        }

        for (int j = 0; j < line.size(); j++) {
            Object value = line.get(j);
            ResultSetMetaDataBase.ColumnMetaData columnMetaData = (ResultSetMetaDataBase.ColumnMetaData)columns.get(j);
            if(value == null || value ==""){
                columnMetaData.setColumnType(12);
                columnMetaData.setColumnTypeName("VARCHAR");
            }
            if ((value instanceof String)) {
                columnMetaData.setColumnType(12);
                columnMetaData.setColumnTypeName("VARCHAR");
            }
            else if (((value instanceof Long)) || ((value instanceof Integer)) || ((value instanceof Short)) || ((value instanceof Byte)) || ((value instanceof Double)) || ((value instanceof Float)))
            {
                columnMetaData.setColumnType(3);
                columnMetaData.setColumnTypeName("DECIMAL");
            }
            else if ((value instanceof Boolean)) {
                columnMetaData.setColumnType(16);
                columnMetaData.setColumnTypeName("BOOLEAN");
            }
            else if ((value instanceof Date)) {
                columnMetaData.setColumnType(91);
                columnMetaData.setColumnTypeName("DATE");
            }
            else if ((value instanceof Timestamp)) {
                columnMetaData.setColumnType(93);
                columnMetaData.setColumnTypeName("TIMESTAMP");
            }
            else if ((value instanceof Time)) {
                columnMetaData.setColumnType(92);
                columnMetaData.setColumnTypeName("TIME");
            }
        }
    }




        public int getColumnCount() throws SQLException { return this.columns.size(); }




        public String getColumnLabel(int column) throws SQLException { return getColumn(column).getColumnLabel(); }




        public String getColumnTypeName(int column) throws SQLException { return getColumn(column).getColumnTypeName(); }



        public ResultSetMetaDataBase.ColumnMetaData getColumn(int column) { return this.columns.get(column - 1); }
}
